<?php

//Global
/* `not display these cates in breadcrumbs (by name) */
$exclude_cat = 'Video,Zone,hcm.24h.com.vn,24h.com.vn,vnexpress.net,kenh14.vn,eva.vn,ione.vnexpress.net,dantri.vn,vov.vn,news.zing.vn,bongdaplus.vn,gamethu.vnexpress.net,sohoa.vnexpress.net,tuoitre.vn,2sao.vn,ngoisao.net,vietnamnet.vn,vtc.vn,dothi.net,batdongsan.com.vn,archi.vn,tinmoi.vn,vntimes.com.vn,tiin.vn,baodatviet.vn';


//setup php for working with Unicode data
mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
mb_http_input('UTF-8');
mb_language('uni');
mb_regex_encoding('UTF-8');
ob_start('mb_output_handler');

function get_snip($str, $len = 100, $tail = '...') 
{
	$str = strip_tags($str);
	if (strlen($str) > $len) {
		$str = mb_substr($str,0,$len) . $tail;
	}
	return $str;
}

function get_first_image($content) 
{
	$index = strpos($content, '<img ');
	if ($index !== false) {
		$content = substr($content, $index + 5);
		$index = strpos($content, 'src="');
		if ($index !== false) {
			$content = substr($content, $index + 5);
			$index = strpos($content, '"');
			if ($index !== false) {
				$content = substr($content, 0, $index);
				return $content;
			}
			return false;
		}
		return false;
	}
	return false;
}

function get_the_thumb($width = 220, $height = 150, $quality = 50, $thumb_class = 'thumb') 
{
	$post_content = get_the_content();
	$thumb = get_first_image($post_content);
	$img = '';
	if ($thumb != false) {
		$img = '<img class="'.$thumb_class.'" src="'.get_template_directory_uri().'/timthumb.php?src='.$thumb.'&w='.$width.'&h='.$height.'&zc=1&q='.$quality.'" alt="'.get_the_title().'" width="'. $width.'" height="'.$height.'" border="0" />';
	} else {
		/* `insert default image if has no image */

	}
	return $img;
}

function the_thumb($width = 220, $height = 150, $quality = 50, $thumb_class = 'thumb') 
{
	echo get_the_thumb($width, $height, $quality, $thumb_class);
}

function breakcrumb()
{	
	global $exclude_cat;
	$delimiter = '<span>.</span>';
	echo '<a href="'.home_url().'">Trang ch&#7911</a>';
	if (is_category() ) {
		
		$cate = get_category(get_query_var('cat'), false);
		if ($cate->parent != 0) {
			echo $delimiter . get_category_parents($cate->parent, TRUE, '');
		}
		echo $delimiter . '<a href="'.home_url().'/category/'.$cate->slug.'">'.$cate->name.'</a>';
	
	} else if (is_single()) {
		
		$post_id = get_the_ID();
		$cates = get_the_category($post_id);
		
		foreach ($cates as $cate) {
			if (strpos($exclude_cat, $cate->name) === false) {
				echo $delimiter . '<a href="'.home_url().'/category/'.$cate->slug.'">'.$cate->name.'</a>';
				break;
			}
		}
		
	}
}

function the_related($id) {
	$cate = get_the_category( $id );
		
	if ($cate) {
		global $exclude_cat;
		foreach ($cate as $cat) {
			if (strpos($exclude_cat, $cat->name) === false) {
				$cat_index = $cat->cat_ID;
				break;
			}
		}
		if ($cat_index != null) {
			$args = array(
					'cat' => $cat_index,
					'post__not_in' => array($id),
					'order' => 'DESC',
					'orderby' => 'rand',
					'showposts' => 4
			);
			query_posts($args);
			if ( have_posts() ):
			?><h2>Xem th&#234;m</h2><?php 
				$i = 0;
				while ( have_posts() ) : the_post();
					if ($i == 2) echo '<div class="clear"></div>';					
					$i++;
					?>
					<div class="item">
					<a href="<?php echo get_permalink();?>">
						<?php the_thumb();?>
					</a>
					<h3><a href="<?php echo get_permalink();?>">
					<?php the_title();?>
					</a></h3>
					<div class="clear"></div>
					</div>
					<?php 
				endwhile;
				?>
				<div class="clear"></div>
				<?php
			endif;
						
		}
	}
}



if ( ! function_exists( 'twentyeleven_comment' ) ) :
/**
 * Template for comments and pingbacks.
*
* To override this walker in a child theme without modifying the comments template
* simply create your own twentyeleven_comment(), and that function will be used instead.
*
* Used as a callback by wp_list_comments() for displaying the comments.
*
* @since Twenty Eleven 1.0
*/
function twentyeleven_comment( $comment, $args, $depth ) {
	$GLOBALS['comment'] = $comment;
	switch ( $comment->comment_type ) :
	case 'pingback' :
	case 'trackback' :
		?>
	<li class="post pingback">
		<p><?php _e( 'Pingback:', 'twentyeleven' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __( 'Edit', 'twentyeleven' ), '<span class="edit-link">', '</span>' ); ?></p>
	<?php
			break;
		default :
	?>
	<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
		<article id="comment-<?php comment_ID(); ?>" class="comment">
				<?php
				$avatar_size = 48;
				if ( '0' != $comment->comment_parent ) {
					$avatar_size = 32;
				}
				echo get_avatar( $comment, $avatar_size );
				?>
			<div class="content">
				<h5 class="author" rel="author">
				<?php 
				echo get_comment_author_link();
				?></h5>
				<div class="body"><?php 
				comment_text();
				if ( $comment->comment_approved == '0' ) : ?>
					<em class="comment-awaiting-moderation">Nh&#7853;n x&#233;t &#273;ang ch&#7901; duy&#7879;t</em>
				<?php endif; ?>		
				</div>
				<div class="app">
				<date><?php echo human_time_diff( get_comment_time('U'), current_time('timestamp') ) . ' ago'; ?></date>
				<?php comment_reply_link( array_merge( $args, array( 'reply_text' => 'Ph&#7843;n h&#7891;i', 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
				</div>
				<div class="clear"></div>
				
			</div>					
			
			
		</article><!-- #comment-## -->
		<div class="clear"></div>

	<?php
			break;
	endswitch;
}
endif; // ends check for twentyeleven_comment()





// CUSTOM ADMIN DASHBOARD BY SCRIPT
function cadmin_global_script()
{
	?>
	<script type="text/javascript">
		var home_url = '<?php echo home_url();?>';
		$ = jQuery;
	</script>
	<?php
}
add_action('admin_head', 'cadmin_global_script');

function cadmin_post_script()
{

	global $pagenow, $typenow;
	if (empty($typenow) && !empty($_GET['post'])) {
		$post = get_post($_GET['post']);
		$typenow = $post->post_type;
	}
	if (is_admin() && $typenow=='post') :
		
		if ($pagenow=='post-new.php' OR $pagenow=='post.php'):
		$temp_url = get_template_directory_uri();
		?>

		<div class="feed-bot">
		<?php
		
		/*INSERT FEEDS DISPLAYER HERE
		 #############################*/
		?>
		<link rel="stylesheet" href="<?php echo $temp_url;?>/css/post.css" type="text/css"/>
		<div class="process-bar-mask">&nbsp;</div>
		<div class="process-bar-container">
			<div class="border">
				<div class="inner">
					<div class="body">
						<div class="bar-border"><div class="bar"></div></div>
						<div class="info"><span>Saving ...</span></div>
					</div>
				</div>
			</div>
		</div>
		<button class="auto-post">Auto Post = Verifing...</button>
		<input type="text" id="custom_fetch"/> <button id="fetch_it">Fetch It</button>
		<div class="feed-channels"></div>
		<div class="feeds-wrapper"></div>
		<div class="feeds-parse" style="display:none"></div>
		<div class="feeds-debug"></div>
				
		
		<script type="text/javascript" src="<?php echo $temp_url; ?>/js/admin_lib.js"></script>
		
		<!-- Script for process feed click -->
		<script type="text/javascript" src="<?php echo $temp_url;?>/js/post.js"></script>
		</div>
		<?php 
		endif;
		
	endif;
} 

add_action('admin_footer', 'cadmin_post_script');

